package java44th.group.service.admin.ad;

import com.github.pagehelper.PageHelper;
import java44th.group.bean.admin.bo.ad.AdCreateBo;
import java44th.group.bean.admin.bo.ad.AdListBo;
import java44th.group.bean.po.MarketAd;
import java44th.group.bean.po.MarketAdExample;
import java44th.group.bean.admin.vo.ad.AdListVo;
import java44th.group.mapper.MarketAdMapper;
import java44th.group.utils.LocalHost;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/11 20:21
 * @Description:
 * @PACKAGE_NAME： java44th.group.service.admin.ad
 */

@Transactional
@Service
public class AdServiceImpl implements AdService {


    @Autowired
    MarketAdMapper marketAdMapper;




    @Override
    public AdListVo queryAdListOrderAndLimit(AdListBo adListBo) {
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        marketAdExample.setDistinct(false);
        marketAdExample.setOrderByClause(adListBo.getSort() + " " + adListBo.getOrder());

        if (!StringUtil.isEmpty(adListBo.getName())) {
            criteria.andNameLike("%" + adListBo.getName() + "%");
        }
        if (!StringUtil.isEmpty(adListBo.getContent())) {
            criteria.andContentLike("%" + adListBo.getContent() + "%");
        }

        long total = marketAdMapper.countByExample(marketAdExample);

        PageHelper.startPage(adListBo.getPage(), adListBo.getLimit());
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        //对url进行拼接
        for (MarketAd marketAd : marketAds) {
            if (!StringUtil.isEmpty(marketAd.getUrl()) && !marketAd.getUrl().startsWith("http:")) {
                marketAd.setUrl(LocalHost.localhost+marketAd.getUrl());
            }
        }

        AdListVo adListVo = new AdListVo(
                (int) total,
                (int) total / adListBo.getLimit() + 1,
                adListBo.getLimit(),
                adListBo.getPage(),
                marketAds
        );
        return adListVo;
    }


    @Override
    public MarketAd createAd(AdCreateBo adCreateBo) {
//        MarketAdExample marketAdExample = new MarketAdExample();
//        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        int position = adCreateBo.getPosition();

        MarketAd marketAd = new MarketAd(
                null,
                adCreateBo.getName(),
                adCreateBo.getLink(),
                adCreateBo.getUrl().replaceAll(LocalHost.localhost, ""),
                (byte) position,
                adCreateBo.getContent(),
                null,
                null,
                adCreateBo.getEnabled(),
                new Date(),
                new Date(),
                null
        );
        marketAdMapper.insertSelective(marketAd);
//        System.out.println(marketAd);
        return marketAd;
    }

    @Override
    public void deleteAd(AdCreateBo adCreateBo) {
        marketAdMapper.deleteByPrimaryKey(adCreateBo.getId());
    }

    @Override
    public MarketAd updateAd(AdCreateBo adCreateBo) {
        MarketAd marketAd = new MarketAd();
        try {
            marketAd.setContent(adCreateBo.getContent());
            marketAd.setName(adCreateBo.getName());
            marketAd.setUrl(adCreateBo.getUrl().replaceAll(LocalHost.localhost,""));
            int position = adCreateBo.getPosition();
            marketAd.setPosition((byte)(position));
            marketAd.setLink(adCreateBo.getLink());
            marketAd.setEnabled(adCreateBo.getEnabled());
            marketAd.setId(adCreateBo.getId());
            marketAd.setUpdateTime(new Date());
            marketAdMapper.updateByPrimaryKey(marketAd);
        } catch (Exception e){
            return null;
        }
        return marketAd;
    }
}
